package com.trinity.orders.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trinity.order.refund.domain.XcRefundOrder;
import com.trinity.orders.constant.OrderConstants;
import com.trinity.orders.domain.XcPayRecord;
import com.trinity.orders.mapper.XcPayRecordMapper;
import com.trinity.orders.service.IXcPayRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

/**
 * 支付记录Service业务层处理
 *
 * @author trinity
 * @date 2023-11-19
 */
@Service
public class XcPayRecordServiceImpl extends ServiceImpl<XcPayRecordMapper, XcPayRecord> implements IXcPayRecordService {
    @Resource
    private XcPayRecordMapper xcPayRecordMapper;

    /**
     * 查询支付记录
     *
     * @param id 支付记录主键
     * @return 支付记录
     */
    @Override
    public XcPayRecord selectXcPayRecordById(Long id) {
        return xcPayRecordMapper.selectXcPayRecordById(id);
    }

    @Override
    public XcPayRecord selectPayRecordNoPayByOrderId(String id) {
        QueryWrapper<XcPayRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", id);
        queryWrapper.eq("status", OrderConstants.TradeState.NOTPAY);
        return xcPayRecordMapper.selectOne(queryWrapper);
    }

    @Override
    public XcPayRecord selectPayRecordSuccessByOrderId(String id) {
        QueryWrapper<XcPayRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", id);
        queryWrapper.eq("status", OrderConstants.TradeState.SUCCESS);
        return xcPayRecordMapper.selectOne(queryWrapper);
    }

    @Override
    public XcPayRecord selectPayRecordPaySuccessByOrderId(String id) {
        QueryWrapper<XcPayRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", id)
                .eq("status", OrderConstants.TradeState.SUCCESS)
                .orderByDesc("update_time")
                .last("LIMIT 1");
        List<XcPayRecord> resultList = xcPayRecordMapper.selectList(queryWrapper);
        if (resultList.isEmpty()) {
            // 处理未找到记录的情况
            return null; // 或者抛出自定义异常，根据需求进行处理
        } else {
            // 从 resultList 中选择或处理合适的记录
            return resultList.get(0); // 这里简单地返回第一条记录，你也可以根据具体逻辑进行选择
        }
    }

    /**
     * 查询支付记录列表
     *
     * @param xcPayRecord 支付记录
     * @return 支付记录
     */
    @Override
    public List<XcPayRecord> selectXcPayRecordList(XcPayRecord xcPayRecord) {
        return xcPayRecordMapper.selectXcPayRecordList(xcPayRecord);
    }

    /**
     * 新增支付记录
     *
     * @param xcPayRecord 支付记录
     * @return 结果
     */
    @Override
    public int insertXcPayRecord(XcPayRecord xcPayRecord) {
        return xcPayRecordMapper.insertXcPayRecord(xcPayRecord);
    }

    /**
     * 修改支付记录
     *
     * @param xcPayRecord 支付记录
     * @return 结果
     */
    @Override
    public int updateXcPayRecord(XcPayRecord xcPayRecord) {
        return xcPayRecordMapper.updateXcPayRecord(xcPayRecord);
    }

    /**
     * 批量删除支付记录
     *
     * @param ids 需要删除的支付记录主键
     * @return 结果
     */
    @Override
    public int deleteXcPayRecordByIds(Long[] ids) {
        return xcPayRecordMapper.deleteXcPayRecordByIds(ids);
    }

    /**
     * 删除支付记录信息
     *
     * @param id 支付记录主键
     * @return 结果
     */
    @Override
    public int deleteXcPayRecordById(Long id) {
        return xcPayRecordMapper.deleteXcPayRecordById(id);
    }

    @Override
    public List<XcPayRecord> selectPayRecordNoPayDuration(int minutes) {
        //minutes 分钟之前的时间
        Instant instant = Instant.now().minus(Duration.ofMillis(minutes));
        QueryWrapper<XcPayRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", OrderConstants.TradeState.NOTPAY);
        queryWrapper.le("create_time", instant);
        List<XcPayRecord> xcPayRecords = xcPayRecordMapper.selectList(queryWrapper);
        return xcPayRecords;
    }

    @Override
    public XcPayRecord selectPayRecordByPaNO(String payNo) {
        QueryWrapper<XcPayRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_no", payNo);
        return xcPayRecordMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateXcPayRecordToRefundByTransactionId(String transactionId, String Status) {
        XcPayRecord payRecord = new XcPayRecord();
        UpdateWrapper<XcPayRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("transaction_id", transactionId);
        updateWrapper.set("status", Status);
        xcPayRecordMapper.update(payRecord, updateWrapper);
    }
}
